// 查岗，公告弹框
var JDialogCg = (function() {
	var dialogSelect = {
        platformId: '',
        areaId: '',
        platformCode: '',
        rtoln: '',
        owersId: ''
    }

    var jdObj = {}

    class jd {
    	// 查岗弹框参数 平台输入框，平台下拉ul，对象select，业户div，业户input，业户下拉ul，信息input
    	constructor(pinputId,pulId,selectObjectId,odivId,oinputId,oulId,infoinputId) {
    		this.pinputId = `#${pinputId}`
    		this.pulId = `#${pulId}`
    		this.selectObjectId = `#${selectObjectId}`
    		this.odivId = `#${odivId}`
    		this.oinputId = `#${oinputId}`
    		this.oulId = `#${oulId}`
    		this.infoinputId = `#${infoinputId}`
    		this.selectObjectArr = [{name:'当前连接下级平台',val:'1'},{name:'下级平台所属的单一业户',val:'2'}]
    		this.selectedVal = '1'
    		this._init()
    	}

    	_init() {
    		// 查岗弹框
		    $(this.odivId).hide()
		    let that = this
		    jdSelect.JdSelectRadioInit(this.selectObjectId,this.selectObjectArr,(select) => {
		    	this.selectedVal = select.val
		        if (this.selectedVal == "2") {
		            $(that.odivId).show()
		            $(".dialog-s-center-h").height(180)
		        } else {
		            $(that.odivId).hide()
		            $(".dialog-s-center-h").height(130)
		        }
		    })
		    // $(this.selectObjectId).change(function() {
		    //     let selectedVal = $(`${that.selectObjectId} option:selected`).val()
		    //     if (selectedVal === "2") {
		    //         $(that.odivId).show()
		    //         $(".dialog-s-center-h").height(180)
		    //     } else {
		    //         $(that.odivId).hide()
		    //         $(".dialog-s-center-h").height(130)
		    //     }
		    // })

			this._autoInputFun(this.pinputId, this.pulId, 
		            "/api/assessMent/selectPlatformAll", {}, "platformName", res => {
		                if (res) {
		                    let html = ''
		                    res.forEach(item => {
		                        html += `<li onclick="dPlatformSelect(this)" 
		                            data-item="${item.name}" data-areaid=${item.areaId}
		                            data-platformcode=${item.platformCode}
		                            data-platformid=${item.id}>${item.name}</li>`
		                    })
		                    $(this.pulId).html(html)
		                }
		            })

		    $(this.oinputId).focus(() => {
	            if (!dialogSelect.areaId) {
	                JPop.alert({msg:"请先选择有效平台。"})
	            }
	        })

	        $(document).click(function (event) {
	            var x1 = $('.input-down-ul'); // 设置目标区域
	            // console.log(event)
	            if (!x1.is(event.target) && x1.has(event.target).length === 0) {
	            	$(".input-down-ul").html('')
	                $(".input-down-ul").hide()
	            }
	        })

    	}

    	_btnSubmit(dialogid,command) {
    		if (!dialogSelect.areaId || !$(this.pinputId).val()) {
	            JPop.alert({msg:"请选择有效平台。"})
	            return
	        }
	        let data = {}
	        // if ($(`${this.selectObjectId} option:selected`).val() === '1') {
	        if (this.selectedVal == '1') {
	            data.platformId = dialogSelect.platformId
	            data.platformCode = dialogSelect.platformCode
	            data.objectId = `${dialogSelect.areaId}${dialogSelect.platformCode}`
	        } else {
	        	if (!dialogSelect.owersId || !$(this.oinputId).val()) {
	        		JPop.alert({msg:"请选择业户。"})
	        		return
	        	}
	            data.objectId = dialogSelect.rtoln
	            data.owersId = dialogSelect.owersId
	        }
	        if (dialogid === 'cgangDialog') {
	        	// data.content = $("#cgin_select option:selected").text()
	        	data.content = $("#dselect_cgxx input").val()
	        } else {
	        	data.content = $(this.infoinputId).val()
	        }
	        if (!data.content) {
	        	JPop.alert({msg:"请输入查岗信息。"})
	        	return
	        }

	        
	        // data.objectType = $(`${this.selectObjectId} option:selected`).val()
	        data.objectType = this.selectedVal

	        jrequest.jsocket(command,data).then(res => {
	        	console.log(res)
	        	$(`#${dialogid}`).hide()
	        	this._resetData()
	        })
    	}
    	

		// 自动获取下拉框
		_autoInputFun(inputId,ulId,url,params,name,callback) {
			$(inputId).bind("input propertychange",function(event){
	            let inputval = $(inputId).val()
	            params[name] = inputval
	            // if (inputval) {
	                $(ulId).show()
	                jrequest.get(url,params).then(res => {
	                    if (res.data.length) {
	                        callback(res.data);
	                    } else {
	                        let html = '<li style="text-align:center;">暂无数据</li>'
	                        $(ulId).html(html)
	                        callback(null)
	                    }
	                })
	            // } else {
	            //     $(ulId).hide()
	            //     $(ulId).html('')
	            // }
	        });
		}
		
		_dPlatformSelect(obj) {
		    $(this.pinputId).val($(obj).data("item"))
		    $(this.pulId).hide()
		    $(this.pulId).html('')

		    dialogSelect.areaId = $(obj).data("areaid")
		   	dialogSelect.platformCode = $(obj).data("platformcode")
		    dialogSelect.platformId = $(obj).data("platformid")

		    this._autoInputFun(this.oinputId, this.oulId, 
		        "/api/owsers/listOwers", {platformId:dialogSelect.platformId}, "owersName", res => {
		            if (res) {
		                let html = ''
		                res.forEach(item => {
		                    html += `<li onclick="dUserSelect(this)" 
		                    data-item="${item.owersName}" data-rtoln=${item.rtoln}
		                    data-owersid=${item.id}>${item.owersName}</li>`
		                })
		                $(this.oulId).html(html)
		            }
		        })
		}
		
		_dUserSelect(obj) {
		    $(this.oinputId).val($(obj).data("item"))
		    $(this.oulId).hide()
		    $(this.oulId).html('')

		    dialogSelect.rtoln = $(obj).data("rtoln")
		    dialogSelect.owersId = $(obj).data("owersid")
		}

		_resetData() {
			dialogSelect = {
		        platformId: '',
		        areaId: '',
		        platformCode: '',
		        rtoln: '',
		        owersId: ''
		    }
		    // $(`${this.selectObjectId} option[value='1']`).attr("selected",true)
		    jdSelect.resetSelectRadio(this.selectObjectId,this.selectObjectArr[0].name)
		    $(this.pinputId).val("")
		    $(this.oinputId).val("")
		    $(this.infoinputId).val("")

		    $('.input-down-ul').hide()
	        $('.input-down-ul').html('')
		}
    }

	return {
		// dialog, 平台input，平台下拉框，对象select，业户div，业户input，业户下拉框，信息
		openDialog(dialogId,pinputId,pulId,selectObjectId,odivId,oinputId,oulId,infoinputId) {
			jdObj = new jd(pinputId,pulId,selectObjectId,odivId,oinputId,oulId,infoinputId)
		    $(`#${dialogId}`).show()
		    $(odivId).hide()
		    $(".dialog-s-center-h").height(130)

		   	
		},
		closeDialog(id) {
		    $(`#${id}`).hide()
		    jdObj._resetData()
		},
		btnSubmit(dialogid,command) {
			jdObj._btnSubmit(dialogid,command)
		},
		dPlatformSelect(obj) {
			jdObj._dPlatformSelect(obj)
		},
		dUserSelect(obj) {
			jdObj._dUserSelect(obj)
		}
	}
}())
function dPlatformSelect(obj) {
	JDialogCg.dPlatformSelect(obj)
}
function dUserSelect(obj) {
	JDialogCg.dUserSelect(obj)
}
